import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.undo.*;
import javax.swing.text.*;
/**
 * La clase Mode sirve como base para definir un modo de operación
 * proporcionando metodos de utilidad para armar las pantallas y 
 * algunos componentes especiales. Cada clase derivada de Mode cuenta
 * con un boton de radio para informar y establecer el modo de 
 * operación. Además, se deben establecer los componentes que seran 
 * agregados en las láminas superior e inferior al ser 
 * seleccionados. <br>
 * La lámina superior se divide en las zonas centro, 
 * norte, sur, este y oeste. Este comportamiento es por que la lámina
 * cuenta con un encargado de disposición de borde. (Vease BorderLayout
 * en la documentación de Java para más información)<br>
 * La zona norte esta destinada al nombre del modo de operación y el
 * estado del paro de emergencia. Las zonas restantes estan disponibles 
 * para el resto de los componentes. En estas zonas los componentes se 
 * agregan primero a una lámina rotulada con borde.<br>
 * La lámina inferior también se divide en cinco zonas. Pero en 
 * esta lámina, los componentes son agrupados en solapas según 
 * la categoria a la que pertenecen y se agregan en la zona centro. 
 * Los componentes especiales sin categoria pueden colocarse en 
 * cualquier otra zona.
 */
public abstract class Mode
{
    /**
     * Crea un objeto de tipo Mode al cual se le establece el 
     * nombre y valor del modo de operación correspondiente. El 
     * nombre 
     * @param name nombre clave del modo de operación.
     * @param mode_ valor del modo de operación.
     */
    public Mode( 
        String name, 
        final Integer mode_)
    {
        dscrptnTop = new ArrayList<Dscrptn>(12);
        dscrptnBottom = new ArrayList<Dscrptn>(12);
        mode = mode_;
        radioB = NewComp.getRadioButton(
            name,
            "titleRButtonTMenu",
            "blue",
            false,
            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    InfoManager.addCommand(KbdCmdId.CmdId.MODE, mode_);
                }
            }
        );
        setDescriptionViewPanel();
        setDescriptionEditablePanel();
    }
    /**
     * Crea un objeto de tipo Mode.
     */
    public Mode()
    {
        dscrptnTop = new ArrayList<Dscrptn>(12);
        dscrptnBottom = new ArrayList<Dscrptn>(12);
        radioB = new JRadioButton();
    }
    /**
     * Inicializa el boton para establecer un modo de operación.
     * @param name nombre clave del modo de operación.
     * @param isSelected establece si el bóton inicia seleccionado.
     * @param listener establece el oyente de eventos.
     */
    public void initRadioButton(String name,  
                                boolean isSelected, 
                                ActionListener listener)
    {
        radioB = NewComp.getRadioButton(
            name,
            "titleRButtonTMenu",
            "blue",
            isSelected,
            listener);
    }
    /*public static void initWorkOffset(int x,int y,int z)
    {
        ((PathPlanning)data.getCategory(Data.Category.PATH_PLANNING)).initWorkOffset(x,y,z);
    }*/
    /*public static void initToolOffset(int lenght, int diameter)
    {
        ((PathPlanning)data.getCategory(Data.Category.PATH_PLANNING)).initToolOffset(lenght,diameter);
    }*/
    /*public static void initData(int id,int value)
    {
        for(int i = 0; i < data.getTotalCategory(); i++)
            if(data.getCategory(i).initData(id,value))
                return;
    }*/
    /**
     * Agrega la descripción de una nueva lámina con marco, la cual se
     * agrega a la lámina superior.
     * @param namePanel nombre del rótulo del marco de la lámina.
     * @param place zona en la cual se colocara.
     * @param position dentro de la zona se especifica una posición
     * especifica.
     * @param description es un arreglo de elementos de tipo Data. 
     * El orden en que aparecen en el arreglo, es el orden en que son 
     * agregados a la lámina.
     * @param anotherComponents un arreglo donde se establece un componente
     * sin categoría. Este arreglo se debe especificar si en
     * el parametro anterior(<code>description</code>) se agrego un
     * Data.OTHER. Si es el caso, solo este coponente se toma en cuenta
     * para ser agregado a la lámina, todos los demas son descartados.
     */
    public void addTopPanels(
        String namePanel,
        Place place,
        GridBagConstraints position,
        Data.Id[] description,
        AnotherComponent[] anotherComponents)
    {
        Dscrptn descriptionPanel = new Dscrptn();
        descriptionPanel.setNamePanel(namePanel);
        descriptionPanel.setPlace(place);
        descriptionPanel.setPositionPanel(position);
        descriptionPanel.setDescriptionPanel(description);
        if(anotherComponents != null)
            for(int i = 0; i < anotherComponents.length; i++)
                descriptionPanel.setAnotherDescription(
                    anotherComponents[i]);
        dscrptnTop.add(descriptionPanel);
    }
    /**
     * Agrega la descripción de una nueva solapa u otro tipo de 
     * componente especial, la cual se agrega a la lámina inferior editable.
     * @param namePanel nombre del rótulo de la solapa. Solo se toma en 
     * cuenta si se agrega una solapa en la zona centro de la lámina.
     * @param place zona en la cual se colocara.
     * @param description es un arreglo de elementos de tipo Data. 
     * El orden en que aparecen en el arreglo, es el orden en que son 
     * agregados a la lámina de la solapa. 
     * @param anotherComponents un arreglo donde se establece un componente
     * sin categoría. Este arreglo se debe especificar si en
     * el parametro anterior(<code>description</code>) se agrego un
     * <code>Data.OTHER</code>. En otro caso debe ser declarado nulo.
     * @param layout establece un administrador de diposición GridLayout.
     * Se pueden agregar láminas vacias para rellenar espacios. Estas láminas
     * extras se deben indicar por separado en el arreglo <code>description</code> 
     * como <code>Data.OTHER</code>  y determinar en el arreglo 
     * <code>anotherComponents</code> como <code>AnotherComponent.PANEL</code>.
     * Se puede declarar como nulo el parámetro <code>layout</code>.
     */
    public void addBottomPanels(
        String namePanel,
        Place place,
        Data.Id[] description,
        AnotherComponent[] anotherComponents,
        GridLayout layout)
    {
        Dscrptn descriptionPanel = new Dscrptn();
        descriptionPanel.setNamePanel(namePanel);
        descriptionPanel.setPlace(place);
        descriptionPanel.setDescriptionPanel(description);
        if(anotherComponents != null)
            for(int i = 0; i < anotherComponents.length; i++)
                descriptionPanel.setAnotherDescription(
                    anotherComponents[i]);
        if(layout != null)
            descriptionPanel.setLayout(layout);
        dscrptnBottom.add(descriptionPanel);
    }
    public static JPanel getViewPanel()
    {
        return viewPanel;
    }
    public static JPanel getEditablePanel()
    {
        return editablePanel;
    }
    public AbstractButton getButton()
    {
        return radioB;
    }
    public static String getFileName()
    {
        String name = fileName;
        fileName = "";
        return name;
    }
    public static String getNewFileName()
    {
        String name = newFileName;
        newFileName = "";
        return name;
    }
    public static String getLineText()
    {
        Document doc = editorArea.getDocument();
        String line = "";
        if(doc.getLength() >= indexLine)
        {
            try
            {
                if((doc.getLength() - indexLine)>20)
                    line = doc.getText(indexLine,20);
                else
                    line = doc.getText(indexLine,doc.getLength() - indexLine);
            }catch(BadLocationException e) 
            {
                return null;
            }
        }
        else
            return null;
        indexLine += 20;
        return line;
    }
    /**
     * Agrega una linea de texto en al área de texto activa.
     * @param line cadena de texto a ser agregada.
     */
    public static void setLineText(String line)
    {
        Document doc;
        String id = data.getValue(Data.Id.OPERATION_MODE);
        if(id.equals("editorMode"))
            doc = editorArea.getDocument();
        else if(id.equals("memoryMode"))
            doc = programArea.getDocument();
        else
            return;
        try 
        {
            doc.insertString(doc.getLength(), line, null); 
        }catch(BadLocationException e) {}
        
    }
    /**
     * Establece los componentes respectivos del modo de operación en
     * las láminas superior e inferior.
     */
    public void setMode()
    {
        pressButton();
        data.setCommandValue( KbdCmdId.CmdId.MODE,mode.intValue());
        setViewComponents();
        setEditableComponents();
        if(mode == KbdCmdId.CmdValue.MODE_FILE_MANAGER)
            browserTableRequestFocus();
    }
    /**
     * Establece los nuevos valores a los datos.
     * @param id identificador del dato
     * @param value valor del dato
     */
    /*public static void setDataValue(int id,int value)
    {
        for(int i = 0; i < data.getTotalCategory(); i++)
            if(data.getCategory(i).setDataValue(id,value))
                return;
    }*/
    /**
     * Establece nuevos valores a los datos que son generados
     * por comandos.
     * @param id identificador del comando de teclado.
     * @param value nuevo valor.
     */
    /*public static void setCommandValue(int id,int value)
    {
        for(int i = 0; i < data.getTotalCategory(); i++)
            if(data.getCategory(i).setCommandValue(id,value))
                return;
    }*/
    /**
     * Actualiza la tabla del administrador de archivos.
     */
    public static void updateTable()
    {
        browserTable.setModel(
            NewComp.getTableModel(
                InfoManager.getName(),
                InfoManager.getSize(),
                InfoManager.getDate()));
    }
    /**
     * Establece la descripción de la lámina superior
     * del modo de operación correspondiente. Se declaran
     * todas las opciones con la función 
     * <code>addTopPanels</code>.
     */
    abstract public void setDescriptionViewPanel();
    /**
     * Establece la descripción de la lámina inferior
     * del modo de operación correspondiente. Se declaran
     * todas las opciones con la función 
     * <code>addBottomPanels</code>.
     */
    abstract public void setDescriptionEditablePanel();
    private void browserTableRequestFocus()
    {
        browserTable.requestFocus();
    }
    private void pressButton()
    {
        if(!radioB.isSelected())
            radioB.setSelected(true);
    }
    private void setViewComponents()
    {
        viewPanel.removeAll();
        panelWest.removeAll();
        panelEast.removeAll();
        panelNorth.removeAll();
        panelSouth.removeAll();
        panelCenter.removeAll();
        JPanel panelBorder;
        String namePanel = "";
        Component component = new JPanel();
        AnotherComponent another;
        int i;
        NewComp.changeFont("default");//machineOperation.getValue(DataCategory.Data.OPERATION_MODE));
        for(Dscrptn description : dscrptnTop)
        {
            Data.Id descriptionPanel[] = description.getDescriptionPanel();
            namePanel = description.getNamePanel();
            if(namePanel.equals("currentPosition"))
                panelBorder = NewComp.getPanel(
                    namePanel,
                    "("+
                    NewComp.getString(
                        //data.getCategory(Data.Category.PATH_PLANNING).getValue(Data.Id.METRIC_SYSTEM)
                        data.getValue(Data.Id.METRIC_SYSTEM)
                    )+
                    ")",
                    "titleBorderTFrame", 
                    new GridLayout(descriptionPanel.length, 1, 0, 0)
                );
            else if(namePanel.equals("feedrate"))
                panelBorder = NewComp.getPanel(
                    namePanel,
                    "("+
                    NewComp.getString(
                        //data.getCategory(Data.Category.PATH_PLANNING).getValue(Data.Id.METRIC_SYSTEM)
                        data.getValue(Data.Id.METRIC_SYSTEM)
                    )+
                    "/min)",
                    "titleBorderTFrame", 
                    new GridLayout(descriptionPanel.length, 1, 0, 0)
                );
            else if(namePanel.equals("programZero"))
                panelBorder = NewComp.getPanel(
                    namePanel,
                    //data.getCategory(Data.Category.PATH_PLANNING).getValue(Data.Id.WORK_OFFSET),
                    data.getValue(Data.Id.WORK_OFFSET),
                    "titleBorderTFrame", 
                    new GridLayout(descriptionPanel.length, 1, 0, 0)
                );
            else if(namePanel.equals("spindle"))
                panelBorder = NewComp.getPanel(
                    namePanel,
                    "(rpm)",
                    "titleBorderTFrame", 
                    new GridLayout(descriptionPanel.length, 1, 0, 0)
                );
            else
                panelBorder = NewComp.getPanel(
                    namePanel,
                    "titleBorderTFrame", 
                    new GridLayout(descriptionPanel.length, 1)
                );
            for(Data.Id componentID : descriptionPanel)
            {
                if(componentID.getCategory() == Data.Category.OTHER)
                {
                    another = description.getAnotherDescription();
                    component = getAnotherComponent(another);
                }
                else
                {
                    component = data.getView(componentID);
                }
                if((componentID.getCategory() == DataCategory.Category.OTHER)
                    || (description.getPlace() == Place.NORTH))
                {
                    panelBorder = (JPanel)component;
                    break;
                }
                else if(component != null)
                    panelBorder.add(component);
            }
            switch(description.getPlace())
            {
                case WEST:
                    panelWest.add(panelBorder,description.getPositionPanel());
                    viewPanel.add(panelWest,BorderLayout.WEST);
                    break;
                case EAST:
                    panelEast.add(panelBorder,description.getPositionPanel());
                    viewPanel.add(panelEast,BorderLayout.EAST);
                    break;
                case NORTH:
                    panelNorth.add(panelBorder,description.getPositionPanel());//component,description.getPositionPanel());
                    viewPanel.add(panelNorth,BorderLayout.NORTH);
                    break;
                case SOUTH:
                    panelSouth.add(panelBorder,description.getPositionPanel());
                    viewPanel.add(panelSouth,BorderLayout.SOUTH);
                    break;
                case CENTER:
                    panelCenter.add(panelBorder,description.getPositionPanel());
                    viewPanel.add(panelCenter,BorderLayout.CENTER);
                    break;
                default:
                    panelCenter.add(panelBorder,description.getPositionPanel());
                    viewPanel.add(panelCenter,BorderLayout.CENTER);
            }
        }
        viewPanel.revalidate();
        viewPanel.repaint();
    }
    private void setEditableComponents()
    {
        editablePanel.removeAll();
        JTabbedPane tabbedPane = new JTabbedPane();
        String namePanel = "";
        Component component = new JPanel();
        Place place;
        AnotherComponent another;
        int i;
        JPanel panel;
        for(Dscrptn description : dscrptnBottom)
        {
            Data.Id descriptionPanel[] = description.getDescriptionPanel();
            namePanel = description.getNamePanel();
            panel = NewComp.getPanel(description.getLayout());
            for(Data.Id componentID : descriptionPanel)
            {
                if(componentID.getCategory() == Data.Category.OTHER)
                {
                    another = description.getAnotherDescription();
                    component = getAnotherComponent(another);
                }
                else
                {
                    component = data.getEditor(componentID);
                }
                if(component != null)
                    panel.add(component);
            }
            place = description.getPlace();
            switch(place)
            {
                case WEST:
                    editablePanel.add(component,BorderLayout.WEST);
                    break;
                case EAST:
                    editablePanel.add(component,BorderLayout.EAST);
                    break;
                case NORTH:
                    
                    break;
                case SOUTH:
                    editablePanel.add(component,BorderLayout.SOUTH);
                    break;
                case CENTER:
                    tabbedPane.addTab( 
                        NewComp.getStringWithFormat(namePanel, "titleTab", "black"), 
                        new JScrollPane(panel)
                    );
                    break;
                default:
                    tabbedPane.addTab( NewComp.getStringWithFormat(namePanel, "titleTab", "black"),panel);
            }
        }
        editablePanel.add(tabbedPane,BorderLayout.CENTER);
        editablePanel.validate();
        editablePanel.repaint();
    }
    private Component getAnotherComponent(AnotherComponent another)
    {
        JPanel panel;
        JPanel panel_Aux1;
        switch(another)
        {
            case PANEL:
                return new JPanel();
            case MDI_BUFFER:
                panel = NewComp.getPanel("mdiBuffer","titleBorderBFrame",new BorderLayout());
                JScrollPane scrollPane = new JScrollPane(mdiArea);
                JPanel p_aux = new JPanel();
                JButton okButton = NewComp.getButton(
                    "ok", 
                    "titleButton", 
                    "blue",
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addMdiString(mdiArea.getText());
                            mdiArea.setText("");
                        }
                    }
                );
                JButton cancelButton = NewComp.getButton(
                    "cancel", 
                    "titleButton", 
                    "blue",
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            mdiArea.setText("");
                        }
                    }
                );
                p_aux.add(okButton);
                p_aux.add(cancelButton);
                panel.add(scrollPane,BorderLayout.CENTER);
                panel.add(p_aux,BorderLayout.SOUTH);
                return panel;
            case PROGRAM_EXECUTION_AREA:
                panel = NewComp.getPanel(
                    "programExecutionArea",
                    NewComp.getString(
                        (String)data.getValue(Data.Id.JOB_NAME)
                    ),
                    "titleBorderTFrame",
                    new BorderLayout()
                );
                programArea = NewComp.getTextArea(30,50,"textAreaTFrame",false);
                programArea.setForeground(Color.WHITE);
                programArea.setBackground(Color.black);
                programArea.setEditable(false);
                programArea.setFocusable(false);
                panel.add(programArea,BorderLayout.CENTER);
                return panel;
            case EDITOR_AREA:
                initEditor();
                editorPanel();
                return editorPanel;
            case TASKBAR:
                taskEditorPanel();
                return panelTaskBrowser;
            case FILE_MANAGER:
                panel = NewComp.getPanel(
                    "fileBrowser",
                    NewComp.getString(
                        (String)data.getValue(Data.Id.JOB_NAME)
                    ),
                    "titleBorderTFrame",
                    new BorderLayout()
                );
                browserPanel();
                panel.add(editorPanel,BorderLayout.CENTER);
                return panel;
            case TASKBAR_FILE_MANAGER:
                taskBrowserPanel();
                return panelTaskBrowser;
            case VALIDATE_CHANGE:
                panel = NewComp.getPanel(new GridLayout(4,1));
                JButton applyChange = NewComp.getButton(
                    "ok",
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            data.applyChange();
                        }
                    }
                );
                JButton cancelChange = NewComp.getButton(
                    "cancel",
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                                data.cancelChange();
                        }
                    }
                );
                panel_Aux1 = new JPanel();
                JPanel panel_Aux2 = new JPanel();
                panel.add(panel_Aux1);
                panel.add(applyChange);
                panel.add(cancelChange);
                panel.add(panel_Aux2);
                return panel;
            default:
                return null;
        }
    }
    private void initEditor()
    {
        undo = new UndoManager();
        nameEditFile.setText(NewComp.getString("new"));
        editorArea = NewComp.getTextArea(20,50,"textAreaTFrame",false);
        editorArea.getDocument().addUndoableEditListener(new 
            UndoableEditListener() {
                public void undoableEditHappened(UndoableEditEvent evt) 
                {
                    undo.addEdit(evt.getEdit());
                }
            }
        );
        editorArea.addFocusListener(new 
            FocusListener()
            {
                public void focusGained(FocusEvent e){}
                public void focusLost(FocusEvent e)
                {
                    JTextArea aux = (JTextArea)e.getSource();
                    if(aux.isFocusable())
                    aux.requestFocus();
                }
            }
        );
        editorArea.getDocument().addDocumentListener(new 
            DocumentListener()
            {
                public void insertUpdate(DocumentEvent event) { isSaved = false;}
                public void removeUpdate(DocumentEvent event) { isSaved = false;}
                public void changedUpdate(DocumentEvent event){ isSaved = false;}
            }
        );
    }
    private void editorPanel()
    {
        editorPanel.removeAll();
        editorPanel.setLayout(new BorderLayout());
        editorPanel.add(nameEditFile,BorderLayout.NORTH);
        editorPanel.add(new JScrollPane(editorArea),BorderLayout.CENTER);
        editorPanel.revalidate();
    }
    private void browserPanel()
    {
        editorPanel.removeAll();
        editorPanel.setLayout(new BorderLayout());
        browserTable = NewComp.getTable(InfoManager.getName(),InfoManager.getSize(),InfoManager.getDate(),"textTable");
        browserTable.addFocusListener(new 
            FocusListener()
            {
                public void focusGained(FocusEvent e){}
                public void focusLost(FocusEvent e)
                {
                    JTable aux = (JTable)e.getSource();
                    aux.requestFocus();
                }
            }
        );
        browserTable.setRowHeight(25);
        JScrollPane scrollTable = new JScrollPane(browserTable);
        editorPanel.add(scrollTable,BorderLayout.CENTER);
        editorPanel.revalidate();
    }
    private void taskEditorPanel()
    {
        panelTaskBrowser.removeAll();
        panelTaskBrowser.setLayout(new GridLayout(1,8));
        JButton newButton = NewComp.getButton(
            "new", 
            "new",
            "titleButton",
            "blue",
            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    ActionListener okListener = new 
                        ActionListener()
                        {
                            public void actionPerformed(ActionEvent event)
                            {
                                nameEditFile.setText(NewComp.getString("new"));
                                isSaved = false;
                                editorArea.setText("");
                                undo.discardAllEdits();
                                taskEditorPanel();
                            }
                        };
                    ActionListener cancelListener = new 
                        ActionListener()
                        {
                            public void actionPerformed(ActionEvent event)
                            {
                                taskEditorPanel();
                            }
                        };
                    confirmDialog("new",okListener,cancelListener);
                }
            }
        );
        JButton openButton = NewComp.getButton(
            "open", 
            "open",
            "titleButton", 
            "blue",
            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    InfoManager.addTask(Task.BROWSER_REFRESH.getValue());
                    browserPanel();
                    ActionListener okListener = new 
                        ActionListener()
                        {
                            public void actionPerformed(ActionEvent event)
                            {
                                if(-1 < browserTable.getSelectedRow())
                                {
                                    editorArea.setText("");
                                    undo.discardAllEdits();
                                    editorPanel();
                                    fileName = (String)browserTable.getValueAt(browserTable.getSelectedRow(),0);
                                    nameEditFile.setText(fileName);
                                    InfoManager.addTask(Task.OPEN.getValue());
                                    taskEditorPanel();
                                    isSaved = true;
                                }
                            }
                        };
                    ActionListener cancelListener = new 
                        ActionListener()
                        {
                            public void actionPerformed(ActionEvent event)
                            {
                                editorPanel();
                                taskEditorPanel();
                            }
                        };
                    confirmDialog("open",okListener,cancelListener);
                    browserTable.requestFocus();
                }
            }
        );
        JButton saveButton = NewComp.getButton(
            "save", 
            "save",
            "titleButton", 
            "blue",
            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    if(isSaved == false)
                    {
                        indexLine = 0;
                        isSaved = true;
                        fileName = nameEditFile.getText();
                        InfoManager.addTask(Task.SAVE.getValue());
                        editorArea.setFocusable(false);
                    }
                }
            }
        );
        JButton saveAsButton = NewComp.getButton(
            "saveAs",
            "saveAs",
            "titleButton", 
            "blue",
            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    ActionListener okListener = new 
                        ActionListener()
                        {
                            public void actionPerformed(ActionEvent event)
                            {
                                newFileName = nameField.getText();
                                fileName = nameEditFile.getText();
                                nameEditFile.setText(newFileName);
                                indexLine = 0;
                                isSaved = true;
                                InfoManager.addTask(Task.SAVEAS.getValue());
                                editorArea.setFocusable(false);
                                taskEditorPanel();
                            }
                        };
                    ActionListener cancelListener = new 
                        ActionListener()
                        {
                            public void actionPerformed(ActionEvent event)
                            {
                                taskEditorPanel();
                            }
                        };
                    editorArea.setFocusable(false);
                    nameDialog(okListener,cancelListener);
                }
            }
        );
        JButton undoButton = NewComp.getButton(
            "undo", 
            "undo",
            "titleButton", 
            "blue",
            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    undoAction();
                }
            }
        );
        JButton redoButton = NewComp.getButton(
            "redo", 
            "redo", 
            "titleButton", 
            "blue",
            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    redoAction();
                }
            }
        );
        JButton editButton = NewComp.getButton(
            "edit", 
            "edit",
            "titleButton", 
            "blue",
            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    if(editorArea.isFocusable())
                        editorArea.setFocusable(false);
                    else
                    {
                        editorArea.setFocusable(true);
                        editorArea.requestFocus();
                    }
                }
            }
        );
        JButton compileButton = NewComp.getButton(
            "compile", 
            "compile",
            "titleButton", 
            "blue",
            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                
                }
            }
        );
        panelTaskBrowser.add(newButton);
        panelTaskBrowser.add(openButton);
        panelTaskBrowser.add(saveButton);
        panelTaskBrowser.add(saveAsButton);
        panelTaskBrowser.add(undoButton);
        panelTaskBrowser.add(redoButton);
        panelTaskBrowser.add(editButton);
        panelTaskBrowser.add(compileButton);
        panelTaskBrowser.revalidate();
    }
    private void taskBrowserPanel()
    {
        panelTaskBrowser.removeAll();
        panelTaskBrowser.setLayout(new GridLayout(1,1));
        JButton copyButton = NewComp.getButton(
            "copy", 
            "copy",
            "titleButton", 
            "blue",
            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    if(-1 < browserTable.getSelectedRow())
                        fileName = (String)browserTable.getValueAt(browserTable.getSelectedRow(),0); 
                }
            }
        );
        JButton pasteButton = NewComp.getButton(
            "paste", 
            "paste",
            "titleButton", 
            "blue",
            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    if(fileName != null)
                        InfoManager.addTask(Task.COPY.getValue());
                }
            }
        );
        JButton deleteButton = NewComp.getButton(
            "delete", 
            "delete",
            "titleButton", 
            "blue",
            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    if(-1 < browserTable.getSelectedRow())
                    {
                        ActionListener okListener = new 
                            ActionListener()
                            {
                                public void actionPerformed(ActionEvent event)
                                {
                                    fileName = (String)browserTable.getValueAt(browserTable.getSelectedRow(),0); 
                                    InfoManager.addTask(Task.DELETE.getValue());
                                    taskBrowserPanel();
                                }
                            };
                        ActionListener cancelListener = new 
                            ActionListener()
                            {
                                public void actionPerformed(ActionEvent event)
                                {
                                    taskBrowserPanel();
                                }
                            };
                        confirmDialog("delete",okListener,cancelListener);
                    }
                }
            }
        );
        JButton renameButton = NewComp.getButton(
            "rename", 
            "rename",
            "titleButton", 
            "blue",
            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    if(-1 < browserTable.getSelectedRow())
                    {
                        browserTable.setFocusable(false);
                        ActionListener okListener = new 
                            ActionListener()
                            {
                                public void actionPerformed(ActionEvent event)
                                {
                                    newFileName = nameField.getText();
                                    fileName = (String)browserTable.getValueAt(browserTable.getSelectedRow(),0); 
                                    InfoManager.addTask(Task.RENAME.getValue());
                                    browserTable.setFocusable(true);
                                    browserTable.requestFocus();
                                    taskBrowserPanel();
                                }
                            };
                        ActionListener cancelListener = new 
                            ActionListener()
                            {
                                public void actionPerformed(ActionEvent event)
                                {
                                    browserTable.setFocusable(true);
                                    browserTable.requestFocus();
                                    taskBrowserPanel();
                                }
                            };
                        nameDialog(okListener,cancelListener);
                    }
                }
            }
        );
        JButton updateButton = NewComp.getButton(
            "update",
            "update",
            "titleButton", 
            "blue",
            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    InfoManager.addTask(Task.BROWSER_REFRESH.getValue());
                }
            }
        );
        JPanel panel_Aux1 = NewComp.getPanel(new GridLayout(1,4));
        panel_Aux1.add(copyButton);
        panel_Aux1.add(pasteButton);
        panel_Aux1.add(deleteButton);
        panel_Aux1.add(renameButton);
        panel_Aux1.add(updateButton);
        panelTaskBrowser.add(panel_Aux1);
        panelTaskBrowser.revalidate();
    }
    private void confirmDialog(final String option,final ActionListener okListener,final ActionListener cancelListener)
    {
        JPanel panel_1 = new JPanel();
        JButton okButton = NewComp.getButton(
            option, "titleButton", "blue", okListener);
        JButton cancelButton = NewComp.getButton(
            "cancel", "titleButton", "blue", cancelListener);
        panelTaskBrowser.removeAll();
        panelTaskBrowser.setLayout(new GridLayout(1,1));
        panel_1.add(okButton);
        panel_1.add(cancelButton);
        panelTaskBrowser.add(panel_1);
        panelTaskBrowser.revalidate();
    }
    private void nameDialog(final ActionListener okListener,
                            final ActionListener cancelListener)
    {
        JPanel panel_1 = new JPanel();
        nameField = NewComp.getTxtFieldEditable("textFieldBFrame");
        JLabel label = NewComp.getLabel("newName", "titleLabelBFrame", "black");
        JButton okButton = NewComp.getButton(
            "ok", "titleButton", "blue", okListener);
        JButton cancelButton = NewComp.getButton(
            "cancel", "titleButton", "blue", cancelListener);
        panelTaskBrowser.removeAll();
        panelTaskBrowser.setLayout(new GridLayout(1,1));
        panel_1.add(label);
        panel_1.add(nameField);
        panel_1.add(okButton);
        panel_1.add(cancelButton);
        panelTaskBrowser.add(panel_1);
        panelTaskBrowser.revalidate();
        nameField.requestFocus();
    }
    private void undoAction()
    {
        try {
          if (undo.canUndo()) {
            undo.undo();
          }
        } catch (CannotUndoException e) {
        }
    }
    private void redoAction()
    {
        try {
          if (undo.canRedo()) {
            undo.redo();
          }
        } catch (CannotRedoException e) {
        }
    }
    private class Dscrptn
    {
        public Dscrptn()
        {
            namePanel = "default";
            positionPanel = new GridBagConstraints();
            descriptionPanel = new Data.Id[] {Data.Id.OTHER};
            place = Place.CENTER;
            another = new ArrayList<AnotherComponent>(5);
            layout = new FlowLayout(FlowLayout.CENTER,16,16);
        }
        public void setNamePanel(String namePanel_)
        {
            namePanel = namePanel_;
        }
        public void setPlace(Place place_)
        {
            place = place_;
        }
        public void setPositionPanel(GridBagConstraints positionPanel_)
        {
            positionPanel = positionPanel_;
        }
        public void setDescriptionPanel(Data.Id[] descriptionPanel_)
        {
            descriptionPanel = descriptionPanel_;
        }
        public String getNamePanel()
        {
            return namePanel;
        }
        public Place getPlace()
        {
            return place;
        }
        public String getStringPlace()
        {
            switch(place)
            {
                case NORTH: 
                    return BorderLayout.NORTH;
                case SOUTH:
                    return BorderLayout.SOUTH;
                case EAST: 
                    return BorderLayout.EAST;
                case WEST: 
                    return BorderLayout.WEST;
                case CENTER:
                    return BorderLayout.CENTER;
                default:
                 return BorderLayout.CENTER;
            }
        }
        public GridBagConstraints getPositionPanel()
        {
            return positionPanel;
        }
        public Data.Id[] getDescriptionPanel()
        {
            return descriptionPanel;
        }
        public void setAnotherDescription(AnotherComponent another_)
        {
            another.add( another_);
        }
        public AnotherComponent getAnotherDescription()
        {
            AnotherComponent another_ = another.get(0);
            another.remove(0);
            another.add( another_);
            return another_;
        }
        public void setLayout(LayoutManager layout_)
        {
            layout = layout_;
        }
        public LayoutManager getLayout()
        {
            return layout;
        }
        private String namePanel;
        private GridBagConstraints positionPanel;
        private Data.Id[] descriptionPanel;
        private Place place;
        private ArrayList<AnotherComponent> another;
        private LayoutManager layout;
    }
    public static enum Place
    {
        NORTH, SOUTH, EAST, WEST, CENTER;
    }
    public static enum AnotherComponent
    {
        NULL,
        MDI_BUFFER,
        PANEL,
        PROGRAM_EXECUTION_AREA,
        EDITOR_AREA,
        TASKBAR,
        FILE_MANAGER,
        TASKBAR_FILE_MANAGER,
        COPY_DIALOG,
        VALIDATE_CHANGE;
    }
    private static enum Task
    {
        COPY(0x00),
        OPEN(0x01),
        SAVE(0x02),
        SAVEAS(0x03),
        BROWSER_REFRESH(0x04),
        DELETE(0x05),
        RENAME(0x06);
        private int command;
        private Task(int c) 
        {
            command = c;
        }
        public int getValue() 
        {
            return command;
        }
    }
    static
    {
        viewPanel = new JPanel(new BorderLayout(16,16));
        panelNorth = new JPanel(new GridBagLayout());
        panelSouth = new JPanel(new GridBagLayout());
        panelWest = new JPanel(new GridBagLayout());
        panelEast = new JPanel(new GridBagLayout());
        panelCenter = new JPanel(new GridBagLayout());
        editablePanel = new JPanel( new BorderLayout());
        browserTable = new JTable();
        fileName = null;
        newFileName = null;
        panelTaskBrowser = new JPanel(new GridLayout(1,1));
        editorPanel = new JPanel(new BorderLayout());
        editorArea = NewComp.getTextArea(30,50,"textAreaTFrame",false);
        programArea = NewComp.getTextArea(30,50,"textAreaTFrame",false);
        mdiArea = NewComp.getTextArea(8,20,"textAreaBFrame",true);
        nameEditFile = NewComp.getLabel("new","textNameFile");
        isSaved = false;
        undo = new UndoManager();
        indexLine = 0;
        data = new Data();
    }
    private static int indexLine;
    private static UndoManager undo;
    private static JLabel nameEditFile;
    private static boolean isSaved;
    private static JTextArea editorArea;
    private JTextField nameField;
    private static JTable browserTable;
    private static JPanel editorPanel;
    private static JPanel panelTaskBrowser;
    private static JScrollPane scrollTable;
    private static JPanel panelTable;
    private static String fileName;
    private static String newFileName;
    private static JPanel panelNorth;
    private static JPanel panelSouth;
    private static JPanel panelWest;
    private static JPanel panelEast;
    private static JPanel panelCenter;
    private static JPanel viewPanel;
    private static JPanel editablePanel;
    private JRadioButton radioB;
    private ArrayList<Dscrptn> dscrptnTop;
    private ArrayList<Dscrptn> dscrptnBottom;
    private Integer mode;
    private static Data data;
    private static JTextArea programArea;
    private static JTextArea mdiArea;
}

